import { combineReducers } from 'redux';

const initialState = {
	data: []
}

let addTodo = (data, action) => {
	return [...data, {
		text: action.value,
		completed: false
	}]
}

let toogleTodo = (data, action) => {
	return data.map((item, i) => {
		if(action.value == i) {
			item.completed = !item.completed
		}
		return item;
	})
}

let deleteTodo = (data, action) => {
	return data.filter((item, i) => {
		return action.value !== i;
	});
}

let todoApp = (data, action) => {
	
	switch(action.type) {
		case 'add':
			return addTodo(data, action);
		case 'toogle':
			return toogleTodo(data, action);
		case 'delete':
			return deleteTodo(data, action);
		default:
			return data;
	}
}

export default function reducer(state = initialState, action) {
	return {
		data: todoApp(state.data, action)
	}
}